import axios from "axios";

export default {
  state: {
    username: "未登录",
    id: "",
    isLogin: false,
    jwt_token: "",
    isPullingInfo: false,
  },
  getters: {},
  mutations: {
    setIsLogin(state, param) {
      state.isLogin = param;
    },
    setUserInfo(state, user) {
      state.username = user.username;
      state.id = user.id;
    },
    updateToken(state, token) {
      state.jwt_token = token;
    },
    logOut(state) {
      state.isLogin = false;
      state.jwt_token = "";
      state.username = "未登录";
      state.id = "";
      localStorage.removeItem("jwt_token");
    },
    getToken(state) {
      return state.jwt_token;
    },
  },
  actions: {
    // 登录 并 获取token
    login(context, data) {
      axios
        .post("/user/login", {
          username: data.username,
          password: data.password,
        })
        .then((resp) => {
          if (resp.data.code == 200) {
            context.commit("updateToken", resp.data.data.token);
            localStorage.setItem("jwt_token", context.state.jwt_token);
            data.success(resp.data);
          } else {
            data.error(resp.data);
          }
        })
        .catch((resp) => {
          data.error(resp);
        });
    },
    // 获取当前用户信息
    getInfo(context, data) {
      context.state.isPullingInfo = true;
      let url = "/user/get/user/info";
      let postData = {};
      let config = {
        headers: {
          Authorization: "Bearer " + context.state.jwt_token,
        },
      };
      axios
        .post(url, postData, config)
        .then((resp) => {
          if (resp.data.code == 200) {
            let info = resp.data.data;
            context.state.username = info.username;
            context.state.id = info.id;
            context.state.name = info.name;
            context.state.phoneNumber = info.phoneNumber;
            context.state.idNum = info.idNum;
            context.state.userInfoId = info.userInfoId;
            data.success(resp.data);
          } else {
            data.error(resp.data);
          }
          context.state.isPullingInfo = false;
        })
        .catch((resp) => {
          data.error(resp);
          context.state.isPullingInfo = false;
        });
    },
    // 注册
    register(context, data) {
      axios
        .post("/user/register", {
          username: data.username,
          password: data.password,
          confirmedPassword: data.confirmedPassword,
        })
        .then((resp) => {
          data.success(resp);
        })
        .catch((resp) => {
          data.error(resp);
        });
    },
    // 更新密码
    updatePassword(context, data) {
      let url = "/user/update/password";
      let postData = data.user;
      let config = {
        headers: {
          Authorization: "Bearer " + data.token,
        },
      };
      axios
        .post(url, postData, config)
        .then((resp) => {
          data.success(resp);
        })
        .catch((resp) => {
          data.error(resp);
        });
    },
  },
  modules: {},
};
